package com.my.biao.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.my.biao.R;
import com.my.biao.base.BaseActivity;
import com.my.biao.constant.BiaoConstant;
import com.my.biao.util.CheckAppInstall;
import com.my.biao.util.GlideManagerUtil;
import com.my.biao.util.LogUtil;
import com.my.biao.util.ToastUtil;
import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQToken;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private Button btnQQ;
    private Button btnWX;
    private Tencent mTencent;
    private ImageView ivHeader;
    private UserInfo mUserInfo;

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_login);
    }

    @Override
    public void initView() {
        btnQQ = findViewById(R.id.btn_login_qq);
        btnWX = findViewById(R.id.btn_login_wx);
        ivHeader = findViewById(R.id.iv_login_header);
        initQQ();
    }

    /**
     * 初始化QQ相关
     */
    private void initQQ() {
        mTencent = Tencent.createInstance(BiaoConstant.QQ_APP_ID, LoginActivity.this);
    }

    @Override
    public void initListener() {
        btnQQ.setOnClickListener(this);
        btnWX.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login_qq: // QQ 登录
                if (CheckAppInstall.checkApp(this, "com.tencent.mobileqq")) { // 检查App是否安装QQ
                    goQQLogin();
                } else {
                    ToastUtil.showShort(this, "请先安装QQ");
                }
                break;
            case R.id.btn_login_wx: // 微信登录
                ToastUtil.showShort(this, "功能暂未开发,敬请期待!");
                break;
        }
    }

    /**
     * 跳转QQ登录
     */
    private void goQQLogin() {
        mTencent.login(this, "all", new QQLoginCallBack());
    }

    /**
     * QQ登录之后的返回
     */
    private class QQLoginCallBack implements IUiListener {

        @Override
        public void onComplete(Object o) {
            Toast.makeText(LoginActivity.this, "登录成功：", Toast.LENGTH_SHORT).show();
            LogUtil.i("QQ登录成功：" + o.toString());

            getUserDetails(o);
        }

        @Override
        public void onError(UiError uiError) {
            Toast.makeText(LoginActivity.this, "登录失败:", Toast.LENGTH_SHORT).show();
            LogUtil.i("QQ登录失败:" + uiError.toString());
        }

        @Override
        public void onCancel() {
            Toast.makeText(LoginActivity.this, "取消登录", Toast.LENGTH_SHORT).show();
            LogUtil.i("QQ登录取消:");
        }
    }

    /**
     * 获取用户详细信息
     */
    private void getUserDetails(Object response) {
        mUserInfo = new UserInfo(this, mTencent.getQQToken());
        JSONObject obj = (JSONObject) response;
        try {
            String openID = obj.getString("openid");
            String accessToken = obj.getString("access_token");
            String expires = obj.getString("expires_in");
            mTencent.setOpenId(openID);
            mTencent.setAccessToken(accessToken, expires);
            QQToken qqToken = mTencent.getQQToken();
            mUserInfo = new UserInfo(getApplicationContext(), qqToken);
            mUserInfo.getUserInfo(new IUiListener() {
                @Override
                public void onComplete(Object response) {
                    LogUtil.i("详细信息请求成功：" + response);

                    try {
                        JSONObject jo = (JSONObject) response;
                        String userHeaderUrl = jo.getString("figureurl_1");
                        GlideManagerUtil.showSimpleImage(LoginActivity.this, ivHeader, userHeaderUrl);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(UiError uiError) {
                    LogUtil.i("详细信息请求失败：" + uiError.toString());
                }

                @Override
                public void onCancel() {
                    LogUtil.i("详细信息请取消：");

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Tencent.onActivityResultData(requestCode, resultCode,
                data, new QQLoginCallBack());

        if (requestCode == Constants.REQUEST_API) {
            if (resultCode == Constants.REQUEST_LOGIN) {
                Tencent.handleResultData(data, new QQLoginCallBack());
            }
        }
    }
}
